package com.hzlh.utils;

import com.alibaba.fastjson.JSON;
import com.hzlh.common.Constants;
import com.hzlh.exception.GlobalException;
import com.hzlh.exception.ResponseCode;
import com.hzlh.model.PcUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户基础信息获取，也可将用户公共信息类移到conmons里，方便其他模块对象调用
 */
@Component
@Slf4j
public class UserInfoUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public static final String PCUSERINFO = "pc_login:";

    @Value("${spring.profiles.active}")
    private String profiles;
    @Value("${casclient.clientHost}")
    private String clientHost;
    /**
     * @Description：获取用户信息
     * @DateTime：2020/10/15 17:10
     * @Params：
     * @Return：
     */
    public PcUserInfo getPcUserInfo(HttpServletRequest request) {
//        if(request.getUserPrincipal()==null) {
//            throw new GlobalException(ResponseCode.UN_LOGIN);
//        }
        if("dev".equals(profiles)){
            String remoteAddr = request.getRemoteAddr();
            if(clientHost.contains(remoteAddr)){
                String str = "{\n" +
                        "  \"adminCode\": \"xtgl001\",\n" +
                        "  \"adminId\": \"d3345183-d3e6-4fde-ba44-e3993715f69a\",\n" +
                        "  \"adminName\": \"系统管理员\",\n" +
                        "  \"app\": null,\n" +
                        "  \"bindPhone\": \"\",\n" +
                        "  \"customerId\": \"\",\n" +
                        "  \"dong\": null,\n" +
                        "  \"headSculpture\": \"upload/img/d3345183-d3e6-4fde-ba44-e3993715f69a_180x180.jpg?p=0.855104525040418\",\n" +
                        "  \"jsStation\": null,\n" +
                        "  \"openId\": \"\",\n" +
                        "  \"orgId\": \"10001\",\n" +
                        "  \"payMode\": 1,\n" +
                        "  \"saler\": false,\n" +
                        "  \"storeName\": \"\",\n" +
                        "  \"storePhone\": \"\",\n" +
                        "  \"token\": \"gh_2b949dcfd84a\"\n" +
                        "}";
                return JSON.parseObject(str,PcUserInfo.class);
            }
        }
        String pcToken = request.getHeader(Constants.PC_TOKEN);
        if(StringUtils.isEmpty(pcToken)){
            pcToken = request.getParameter(Constants.PC_TOKEN);
        }
        if(StringUtils.isEmpty(pcToken)){
            throw new GlobalException(ResponseCode.UN_LOGIN);
        }
        String userInfoStr = this.stringRedisTemplate.opsForValue().get(PCUSERINFO + pcToken);
        if (StringUtils.isEmpty(userInfoStr)) {
            throw new GlobalException(ResponseCode.UN_LOGIN);
        }
        return JSON.parseObject(userInfoStr,PcUserInfo.class);
    }
}
